<template>
  <div class="login-page">
    <!-- 背景图片层 -->
    <div class="login-bg"></div>
    
    <!-- 半透明遮罩层 -->
    <div class="login-overlay"></div>
    
    <!-- 登录表单容器 -->
    <div class="login-wrapper">
      <el-form 
        :model="ruleForm2" 
        :rules="rules2" 
        ref="ruleForm2" 
        label-position="left" 
        label-width="0px"
        class="demo-ruleForm login-container"
      >
        <div class="login-header">
          <div class="login-logo">
            <i class="el-icon-lock"></i>
          </div>
          <h3 class="title">系统登录</h3>
          <p class="subtitle">请输入账号密码登录系统</p>
        </div>
        
        <el-form-item prop="account">
          <el-input 
            type="text" 
            v-model="ruleForm2.account" 
            auto-complete="off" 
            placeholder="账号"
            prefix-icon="el-icon-user"
            class="custom-input"
          ></el-input>
        </el-form-item>
        
        <el-form-item prop="checkPass">
          <el-input 
            type="password" 
            v-model="ruleForm2.checkPass" 
            auto-complete="off" 
            placeholder="密码"
            prefix-icon="el-icon-lock"
            class="custom-input"
          ></el-input>
        </el-form-item>
        
        <div class="login-options">
          <el-checkbox v-model="checked" class="remember">记住密码</el-checkbox>
          <a href="#" class="forgot-password">忘记密码?</a>
        </div>
        
        <el-form-item style="width:100%;">
          <el-button 
            type="primary" 
            style="width:100%;" 
            @click.native.prevent="handleSubmit2"
            :loading="logining"
            class="login-btn"
          >
            <span v-if="!logining">登录</span>
            <span v-else>登录中...</span>
          </el-button>
        </el-form-item>
        
        <div class="register-link">
          <span>还没有账号? </span>
          <a href="#" @click.prevent="register">立即注册</a>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import { requestLogin } from '../api/api';
import jwt from 'jsonwebtoken';

export default {
  data() {
    return {
      logining: false,
      ruleForm2: {
        account: '东邪',
        checkPass: 'admin'
      },
      rules2: {
        account: [
          { required: true, message: '请输入账号', trigger: 'blur' }
        ],
        checkPass: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      },
      checked: true
    };
  },
  methods: {
    register() {
      // 跳转到公司入驻界面
      this.$router.push({ path: '/register' });
    },
    handleReset2() {
      this.$refs.ruleForm2.resetFields();
    },
    // 处理登录请求
    handleSubmit2(ev) {
      this.$refs.ruleForm2.validate((valid) => {
        if (valid) {
          this.logining = true;
          var loginParams = {
            account: this.ruleForm2.account,
            password: this.ruleForm2.checkPass,
            type: 0 // 0表示管理员
          };

          this.$http.post("/logininfo/login", loginParams).then(res => {
            if (res.data.success) {
              let token = res.data.data;
              console.log("token:", token);

              let resultJwtJson = jwt.decode(token);
              console.log("resultJwtJson:", resultJwtJson);

              // 存储登录信息到本地存储
              localStorage.setItem("token", token);
              localStorage.setItem("logininfo", JSON.stringify(resultJwtJson.payload.logininfo));
              localStorage.setItem("permissions", JSON.stringify(resultJwtJson.payload.permissions));
              localStorage.setItem("menus", JSON.stringify(resultJwtJson.payload.menus));
              
              this.$message({
                message: '登录成功',
                type: 'success'
              });
              this.$router.push({ path: '/echarts' });
              location.reload();
              
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(res => {
            this.$message.error("系统繁忙，请稍后重试");
          }).finally(() => {
            this.logining = false;
          });
        } else {
          console.log('提交数据有误');
          return false;
        }
      });
    }
  }
}
</script>

<style lang="scss" scoped>
.login-page {
  position: relative;
  width: 100%;
  height: 100vh;
  overflow: hidden;
}

// 背景图片样式
.login-bg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image: url('https://picsum.photos/id/1048/1920/1080'); // 背景图片
  background-size: cover;
  background-position: center;
  z-index: 1;
}

// 半透明遮罩，增强文字可读性
.login-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.4);
  z-index: 2;
}

// 登录表单容器，实现居中显示
.login-wrapper {
  position: relative;
  z-index: 3;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  padding: 20px;
}

// 登录表单卡片样式
.login-container {
  width: 380px;
  padding: 40px 35px 30px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 12px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
  backdrop-filter: blur(10px);
  transition: all 0.3s ease;
  
  &:hover {
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.3);
    transform: translateY(-5px);
  }
}

// 登录头部样式
.login-header {
  text-align: center;
  margin-bottom: 30px;
  
  .login-logo {
    width: 70px;
    height: 70px;
    margin: 0 auto 20px;
    background-color: #409EFF;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    font-size: 30px;
    box-shadow: 0 4px 15px rgba(64, 158, 255, 0.4);
  }
  
  .title {
    margin: 0 0 10px;
    color: #303133;
    font-size: 24px;
    font-weight: 600;
  }
  
  .subtitle {
    margin: 0;
    color: #606266;
    font-size: 14px;
  }
}

// 自定义输入框样式
.custom-input {
  height: 46px;
  border-radius: 23px;
  padding: 0 20px;
  font-size: 15px;
  transition: all 0.3s ease;
  
  &:focus {
    border-color: #409EFF;
    box-shadow: 0 0 0 3px rgba(64, 158, 255, 0.2);
  }
}

// 登录选项区域（记住密码和忘记密码）
.login-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 20px 0 30px;
  
  .remember {
    color: #606266;
    font-size: 14px;
    
    /deep/ .el-checkbox__label {
      padding-left: 5px;
    }
  }
  
  .forgot-password {
    color: #409EFF;
    font-size: 14px;
    text-decoration: none;
    transition: color 0.2s;
    
    &:hover {
      color: #66b1ff;
      text-decoration: underline;
    }
  }
}

// 登录按钮样式
.login-btn {
  height: 46px;
  border-radius: 23px;
  font-size: 16px;
  font-weight: 500;
  background-color: #409EFF;
  border-color: #409EFF;
  transition: all 0.3s ease;
  
  &:hover {
    background-color: #66b1ff;
    border-color: #66b1ff;
    transform: translateY(-2px);
  }
  
  &:active {
    transform: translateY(0);
  }
}

// 注册链接样式
.register-link {
  text-align: center;
  margin-top: 25px;
  font-size: 14px;
  
  span {
    color: #606266;
  }
  
  a {
    color: #409EFF;
    text-decoration: none;
    margin-left: 5px;
    transition: color 0.2s;
    
    &:hover {
      color: #66b1ff;
      text-decoration: underline;
    }
  }
}

// 响应式调整
@media (max-width: 480px) {
  .login-container {
    width: 100%;
    padding: 30px 20px 20px;
  }
}
</style>
